package com.zprmb.udzt.adapter;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zprmb.udzt.Back_money;
import com.zprmb.udzt.R;
import com.zprmb.udzt.ProgressBar.V4RoundProgressBar;
import com.zprmb.udzt.bean.SanBiao;
import com.zprmb.udzt.util.Regular;

public class Regular_Adapter4 extends BaseAdapter {
	private Context context;
	private List<SanBiao> list;
	private String title;
	DecimalFormat df = new DecimalFormat("###0.00");
    private long minute=0;
    private long second=0;
    Help h=null;
    long keyong=0;
    long cha=-1000;
	private boolean flag=false;
	
    
	
	


	public Regular_Adapter4(Context context, List<SanBiao> list, String title,
			boolean flag) {
		super();
		this.context = context;
		this.list = list;
		this.title = title;
		this.flag = flag;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if(convertView==null){
			h=new Help();			
			
			if(title.equals("募集中")){
				convertView=LayoutInflater.from(context).inflate(R.layout.sanbiao_item2, null);
			    
			}else if(title.equals("已满标")){
				convertView=LayoutInflater.from(context).inflate(R.layout.sanbiao_item2, null);
			  
			} else if(title.equals("回款中")){
				convertView=LayoutInflater.from(context).inflate(R.layout.sanbiao_item, null);
			}else if(title.equals("已回款")){
				convertView=LayoutInflater.from(context).inflate(R.layout.sanbiao_item, null);
			}
			
			h.tv_title=(TextView)convertView.findViewById(R.id.conduct_name);
			h.tv_addrate=(TextView)convertView.findViewById(R.id.conduct_addrate);
			h.tv_out_money=(TextView)convertView.findViewById(R.id.ying_outmoney);
			h.invest_info=(TextView)convertView.findViewById(R.id.invest_info);
			h.product_info=(TextView)convertView.findViewById(R.id.product_info);
			h.tv_investamount=(TextView)convertView.findViewById(R.id.amount);
			h.li_out_money=(LinearLayout)convertView.findViewById(R.id.li_out_money);
			h.rl_progress=(RelativeLayout)convertView.findViewById(R.id.rl_progress);
			h.progressBar=(V4RoundProgressBar)convertView.findViewById(R.id.roundProgressBar);
			h.tv_jindu=(TextView)convertView.findViewById(R.id.round_jindu);
			h.tv_status=(TextView)convertView.findViewById(R.id.out_money_status);
			h.tv_back_plan=(TextView)convertView.findViewById(R.id.out_money);
			h.rl_back_money=(RelativeLayout)convertView.findViewById(R.id.rl_back_money);
			h.li_layout=(LinearLayout)convertView.findViewById(R.id.li_layout);
			h.tv_v4_productdetails_day = (TextView) convertView.findViewById(R.id.tv_v4_productdetails_day);
			h.tv_v4_productdetails_hour = (TextView)convertView.findViewById(R.id.tv_v4_productdetails_hour);
			h.tv_v4_productdetails_minutes = (TextView) convertView.findViewById(R.id.tv_v4_productdetails_minutes);
			h.tv_v4_productdetails_second = (TextView)convertView.findViewById(R.id.tv_v4_productdetails_second);
			h.li_count=(LinearLayout)convertView.findViewById(R.id.li_count);
			convertView.setTag(h);
		}else{
			h=(Help) convertView.getTag();
		}
		if(title.equals("募集中")){
			h.tv_investamount.setText(df.format(Double.parseDouble(list.get(position).getInvestAmount())));
			h.li_out_money.setVisibility(View.GONE);
			h.rl_progress.setVisibility(View.VISIBLE);
			h.tv_jindu.setText(list.get(position).getProcess()+"%");
			h.progressBar.setMax(1000);
			h.progressBar.setProgress(Integer.parseInt(String.valueOf(
					list.get(position).getProcess() * 10).replace(".0", "")));
			h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getSubmitTime())+"投资/");
			h.product_info.setText("项目信息:"+"预期年化收益率"+df.format(Double.parseDouble(list.get(position).getLoanRate())/100)+"%/"+list.get(position).getLoanPeriod()+"/"+list.get(position).getRepayMethodTypeKey());
		    h.rl_back_money.setVisibility(View.GONE);
		  
		   
		   countdown(position);
		}else if(title.equals("已满标")){
			h.tv_investamount.setText(df.format(Double.parseDouble(list.get(position).getInvestAmount())));
			h.li_out_money.setVisibility(View.GONE);
			h.rl_progress.setVisibility(View.VISIBLE);
			h.progressBar.setMax(1000);
			h.tv_jindu.setText(list.get(position).getProcess()+"%");
			h.progressBar.setProgress(Integer.parseInt(String.valueOf(
					list.get(position).getProcess() * 10).replace(".0", "")));
			h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getSubmitTime())+"投资/");
			h.product_info.setText("项目信息:"+"预期年化收益率"+df.format(Double.parseDouble(list.get(position).getLoanRate())/100)+"%/"+list.get(position).getLoanPeriod()+"/"+list.get(position).getRepayMethodTypeKey());
			h.rl_back_money.setVisibility(View.GONE);
			
			h.tv_v4_productdetails_day.setText("0");
			h.tv_v4_productdetails_hour.setText("0");
			h.tv_v4_productdetails_minutes.setText("0");
			h.tv_v4_productdetails_second.setText("0");
		} else if(title.equals("回款中")){
			h.li_count.setVisibility(View.GONE);
			h.tv_investamount.setText(df.format(Double.parseDouble(list.get(position).getInvestAmount())));
			h.li_out_money.setVisibility(View.VISIBLE);
			h.rl_progress.setVisibility(View.GONE);
			h.tv_out_money.setText(list.get(position).getRepayAmount());
			h.tv_status.setText("应回款金额(元)");
			h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getSubmitTime())+"投资/"+Regular.getMilliToDate3(list.get(position).getRepayDate())+"到期");
			h.product_info.setText("项目信息:"+"预期年化收益率"+df.format(Double.parseDouble(list.get(position).getLoanRate())/100)+"%/"+list.get(position).getLoanPeriod()+"/"+list.get(position).getRepayMethodTypeKey());			
			h.rl_back_money.setVisibility(View.VISIBLE);
		}else if(title.equals("已回款")){
			h.li_count.setVisibility(View.GONE);
			h.tv_investamount.setText(df.format(Double.parseDouble(list.get(position).getInvestAmount())));
			h.li_out_money.setVisibility(View.VISIBLE);
			h.rl_progress.setVisibility(View.GONE);
			h.tv_out_money.setText(list.get(position).getRepayAmount());
			h.tv_status.setText("回款金额(元)");
			h.invest_info.setText("投资信息:"+Regular.getMilliToDate3(list.get(position).getSubmitTime())+"投资/"+Regular.getMilliToDate3(list.get(position).getRepayDate())+"到期");
			h.product_info.setText("项目信息:"+"预期年化收益率"+df.format(Double.parseDouble(list.get(position).getLoanRate())/100)+"%/"+list.get(position).getLoanPeriod()+"/"+list.get(position).getRepayMethodTypeKey());
			h.rl_back_money.setVisibility(View.VISIBLE);
		}
		h.tv_title.setText(list.get(position).getLoan().getLoanTitle());
		
		h.tv_back_plan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(context,Back_money.class);
				intent.putExtra("investId", list.get(position).getInvestId());
				intent.putExtra("type", 2);
				context.startActivity(intent);
			}
		});
		
		return convertView;
	}
	public String ondata(){
		SimpleDateFormat    formatter    =   new    SimpleDateFormat    ("ddHHmmss");       
		Date    curDate    =   new    Date(System.currentTimeMillis());//获取当前时间       
		String    str    =    formatter.format(curDate);
		return str;
	}
	private void countdown(int position){
		flag=false;
		long now = new Date().getTime();
		long end = list.get(position).getLoan().getOutTimes()*3600*1000+list.get(position).getLoan().getOpenTime();
		keyong=end-now;
//		keyong=396945876;
//		keyong=542420794;
//		if(!list.get(position).getRemOutTimes().equals("")){			
//			
//			 keyong = Long.parseLong(list.get(position).getRemOutTimes())-cha;
//		}else{
//			 keyong=0;
//		}
		Log.e("信息", list.get(position).getRemOutTimes());
		Log.e("可用时间", keyong+"");
		if (keyong > 0 ) {
			
			long day = keyong / (24 * 60 * 60 * 1000);
			long hour = (keyong / (60 * 60 * 1000) - day * 24);
			long min = ((keyong / (60 * 1000)) - day * 24 * 60 - hour * 60);
			@SuppressWarnings("unused")
			long s = (keyong / 1000 - day * 24 * 60 * 60 - hour
					* 60 * 60 - min * 60);
			// long ms = (keyong - day * 24 * 60 * 60 * 1000 - hour*
			// 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
			// System.out.println(day + "天" + hour + "小时" + min +
			// "分"+ s + "秒" + ms + "毫秒");
			h.tv_v4_productdetails_day.setText(day + "");
			h.tv_v4_productdetails_hour.setText(hour + "");
			h.tv_v4_productdetails_minutes.setText(min + "");
			h.tv_v4_productdetails_second.setText("" + s);
			minute = min;
			second = s;
			Message msg = new Message();
			msg.what = 0;
			handler.sendMessage(msg);

			

		}else {
			h.tv_v4_productdetails_day.setText("0");
			h.tv_v4_productdetails_hour.setText("0");
			h.tv_v4_productdetails_minutes.setText("0");
			h.tv_v4_productdetails_second.setText("0");
			
		}
	}
	 class Help{
		 private TextView tv_title,tv_addrate,tv_investamount,tv_jindu,invest_info,product_info,tv_out_money,tv_status,tv_back_plan;
		 private TextView tv_v4_productdetails_day,tv_v4_productdetails_hour,tv_v4_productdetails_minutes,tv_v4_productdetails_second;
		 private V4RoundProgressBar progressBar;
		 private LinearLayout li_out_money,li_layout,li_count;
		 private RelativeLayout rl_progress,rl_back_money;
	 }
	 Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 0) {
					if (minute == 0) {
						if (second == 0) {

//							if (timer != null) {
//								timer.cancel();
//								timer = null;
//							}
//							if (timerTask != null) {
//								timerTask = null;
//							}
						} else {
							second--;
							if (second >= 10) {
								// timeView.setText("0"+minute + ":" + second);
								h.tv_v4_productdetails_minutes.setText(minute + "");
								h.tv_v4_productdetails_second.setText(second + "");
							} else {
								// timeView.setText("0"+minute + ":0" + second);
								h.tv_v4_productdetails_minutes.setText("0" + minute);
								h.tv_v4_productdetails_second.setText("0" + second);
							}
						}
					} else {
						if (second == 0) {
							second = 59;
							minute--;
							if (minute >= 10) {
								// timeView.setText(minute + ":" + second);
								h.tv_v4_productdetails_minutes.setText(minute + "");
								h.tv_v4_productdetails_second.setText(second + "");
							} else {
								// timeView.setText("0"+minute + ":" + second);
								h.tv_v4_productdetails_minutes.setText("0" + minute);
								h.tv_v4_productdetails_second.setText(second + "");
							}
						} else {
							second--;
							if (second >= 10) {
								if (minute >= 10) {
									// timeView.setText(minute + ":" + second);
									h.tv_v4_productdetails_minutes.setText(minute
											+ "");
									h.tv_v4_productdetails_second
											.setText(second + "");
								} else {
									// timeView.setText("0"+minute + ":" + second);
									h.tv_v4_productdetails_minutes.setText("0"
											+ minute);
									h.tv_v4_productdetails_second
											.setText(second + "");
								}
							} else {
								if (minute >= 10) {
									// timeView.setText(minute + ":0" + second);
									h.tv_v4_productdetails_minutes.setText(minute
											+ "");
									h.tv_v4_productdetails_second.setText("0"
											+ second);
								} else {
									// timeView.setText("0"+minute + ":0" + second);
									h.tv_v4_productdetails_minutes.setText("0"
											+ minute);
									h.tv_v4_productdetails_second.setText("0"
											+ second);

								}
							}
						}
					}

				}
			}
		};

}
